//
//  ViewController.m
//  UITextViewLessonTwo
//
//  Created by lcy on 15/12/27.
//  Copyright © 2015年 lcy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UITextViewDelegate>

@property (strong, nonatomic) UITextView *textView;
@property (strong, nonatomic) UILabel *placeHolderLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 50) textContainer:nil];
    self.textView.font = [UIFont systemFontOfSize:12];
    self.textView.backgroundColor = [UIColor redColor];
    self.textView.delegate = self;
    
    self.placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
    self.placeHolderLabel.text = @"请输入你想要的内容";
    self.placeHolderLabel.font = [UIFont systemFontOfSize:12];
    self.placeHolderLabel.textColor = [UIColor lightGrayColor];
    
    [self.textView addSubview:self.placeHolderLabel];
    
    [self.view addSubview:self.textView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapedView)];
    
    [self.view addGestureRecognizer:tap];
}

- (void)tapedView
{
    [self.view endEditing:YES];
    
    if(self.textView.text.length == 0)
    {
        self.placeHolderLabel.hidden = NO;
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"%@", textView.text);
    
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.text.length != 0)
    {
        self.placeHolderLabel.hidden = YES;
    }
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
